package com.yang.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.yang.dao.FielDao;
import com.yang.dao.MiniDao;
import com.yang.entity.Fiel;

public class FileOffLineAction extends ActionSupport {

	private static final long serialVersionUID = 7578293007833359626L;

	private String name;
	private String uri;
	private String father;

	@Override
	public String execute() throws Exception {
		return SUCCESS;
	}

	UUID u = UUID.randomUUID();
	DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	String time = df.format(new Date());

	public String downLoad() throws Exception {
		URL url = new URL(uri);
		URLConnection uc = url.openConnection();
		InputStream is = uc.getInputStream();
		String roundName = u.toString() + name;
		String extension=uri.substring(uri.lastIndexOf(".")+1);
		String contentType=new MiniDao().getMiniTye(extension);
		long length = 0;
		OutputStream os = new FileOutputStream(ServletActionContext.getServletContext().getRealPath("/")+ "temp/"+ roundName);
		int len = 0;
		byte[] b = new byte[1024];
		while ((len = is.read(b)) != -1) {
			length += b.length;
			os.write(b, 0, len);
		}
		os.flush();
		os.close();
		is.close();
		 is=new FileInputStream(ServletActionContext.getServletContext().getRealPath("/")+ "temp/"+ roundName);
		FielDao fd = new FielDao();
		String md = DigestUtils.md5Hex(is);
		is.close();
		Fiel fiel=fd.findByMD5(md);
		Fiel f = new Fiel();
		f.setContentType(contentType);
		f.setMd(md);
		f.setLength(length);
		f.setTime(time);
		f.setSon(-2);
		f.setFather(Integer.parseInt(father == null ? "0" : father));
		f.setName(name);
		if(fiel==null){
			is=new FileInputStream(ServletActionContext.getServletContext().getRealPath("/")+ "temp/"+ roundName);
			f.setRoundName(roundName);
			os=new FileOutputStream(ServletActionContext
					.getServletContext().getRealPath("/")
					+ "download/"
					+ roundName);
			b=new byte[1020];
			len=0;
			while((len=is.read(b))!=-1){
				os.write(b, 0, len);
			}
			os.flush();
			os.close();
			is.close();
			File file=new File(ServletActionContext.getServletContext().getRealPath("/")+ "temp/"+ roundName);
			boolean boo=file.delete();
			if(boo){
				System.out.println("刪除成功");
			}
		}else{
			File file=new File(ServletActionContext.getServletContext().getRealPath("/")+ "temp/"+ roundName);
			boolean boo=file.delete();
			if(boo){
				System.out.println("刪除成功");
			}
			f.setRoundName(fiel.getRoundName());
			f.setContentType(fiel.getContentType());
		}
		return fd.insert(f)?SUCCESS:ERROR;
	}

	public String getName() {
		return name;
	}

	public String getUri() {
		return uri;
	}

	public void setName(String name) {
		this.name = name;
	}

	public void setUri(String uri) {
		this.uri = uri;
	}

	public String getFather() {
		return father;
	}

	public void setFather(String father) {
		this.father = father;
	}

}
